package aaaBlogger.img;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

 







import com.attilaax.encode.EncodeX;
import com.attilax.cmsPoster.BlogPubberAbs;
import com.attilax.cmsPoster.BlogPubberMainform;
import com.attilax.cmsPoster.Ipub;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.Global;
import com.attilax.secury.aes;
import com.attilax.util.PropX;


/**
 * aaaBlogger.CnblogsPubber
 * @author Administrator
 *
 */
@SuppressWarnings("all")
public class SinaPubber  extends BlogPubberAbs implements Ipub{
	 FirefoxDriver driverPa3;
	@Override
	public void pub2(String p1tit, String p2_txt, FirefoxDriver driver) {
		
		String t = getAuthorTxt("sina");
		p2_txt=p2_txt.replace("$attilax_blog_sign$", t);
		
		p2_txt = changeBlogLink(p2_txt);
		
		try {
			new Select(driver.findElement(By.id("componentSelect")))
			.selectByVisibleText("it技术");
		} catch (Exception e) {
			// TODO: handle exception
		}
	
		// driver.findElement(By.cssSelector("option[value=\"1\"]")).click();

		driver.findElement(By.id("articleTitle")).clear();
		driver.findElement(By.id("articleTitle")).sendKeys(p1tit);
		try {
			driver.findElement(By.id("SinaEditorTextarea")).sendKeys(p2_txt);
			// driver.findElement(By.id("editor")).
		} catch (ElementNotVisibleException e) {
			// (IJavaScriptExecutor)
			JavascriptExecutor jse = (JavascriptExecutor) driver;
			p2_txt = EncodeX.jsEncodeSingleQuoue(p2_txt);
			// p2_txt="test in html";
			String baseDir=pathx.classPathParent_jensyegeor()
					 + "/blogger";
			String js =filex.read(baseDir +"/" + Global.blogName.get()
					 
					+ "/editor__form.js");
			String script = js.replace("@txt", p2_txt);

			// window.document.getElementById('editor').value='..xxx'
			driverPa3=driver;
			executeScriptPa3(script);
		
			// jse.ex
		}

//		driver.findElement(By.id("chk_tag_")).click();
//		driver.findElement(By.id("radChl3")).click();
//		driver.findElement(By.id("chkHome")).click();
//		driver.findElement(By.id("txtCheckCode")).click();
//		driver.findElement(By.id("txtCheckCode")).clear();
//		driver.findElement(By.id("txtCheckCode")).sendKeys("12");
		closeTips();
		closeTips_aft_pub();
		
	}

private void closeTips_aft_pub() {
	try {
		  driverPa3.findElement(By.linkText("确定")).click();
		
		 
		} catch (Exception e) {
			
		 
			e.printStackTrace();
		}
		
		
	}

private void closeTips() {
	
	try {
	// driverPa3.findElement(By.className("b_btn")) .click();
	
	 
	} catch (Exception e) {
		
		// driverPa3.findElement(By.className("b_close")).click();
		e.printStackTrace();
	}
	

	try {
	  driverPa3.findElement(By.className("b_close")).click();
	
	 
	} catch (Exception e) {
		
	 
		e.printStackTrace();
	}
	
 
	
	
//	try {
//		JavascriptExecutor js = (JavascriptExecutor) driverPa3;    
//		String myjs="document.getElementsByClassName('b_old_cont')[0].style.display='none';";
//		js.executeScript(myjs);
//		
//		String css2="div[style=\"position: fixed; width: 1409px; height: 1301px; left: 0px; top: 0px; z-index: 0; background-color: rgb(0, 0, 0); opacity: 0.2;\"]";
//		String css3="iframe[style=\"opacity: 0; position: absolute; z-index: 0; left: 0px; top: 0px; width: 1409px; height: 1301px;\"]";
//	//	driverPa3.findElement(By.cssSelector(css2))
//	} catch (Exception e) {
//		e.printStackTrace();
//	}
//	//[name=”username”]
//	String css2="div[style=\"position: fixed; width: 1409px; height: 1301px; left: 0px; top: 0px; z-index: 0; background-color: rgb(0, 0, 0); opacity: 0.2;\"]";
//	String css3="iframe[style=\"opacity: 0; position: absolute; z-index: 0; left: 0px; top: 0px; width: 1409px; height: 1301px;\"]";
//	}
	 
}



/**
 * 
 * @param script
 */
	private void executeScriptPa3(String script) {
		// TODO Auto-generated method stub
		try {
			System.out.println(script);
			driverPa3.executeScript(script,
					driverPa3.findElement(By.id("articleTitle")));
		} catch (org.openqa.selenium.WebDriverException e) {
			script=script.replace("frames[2]", "frames[1]");
			System.out.println(script);
			driverPa3.executeScript(script,
					driverPa3.findElement(By.id("articleTitle")));
		}
	
		
	//	executeScript
	}

	@Override
	public void login(FirefoxDriver driver,Object  px2) {
		PropX px=(PropX) px2;
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(px.getProperty("user"));
		driver.findElement(By.id("password")).clear();
		
		String baseDir=pathx.classPathParent_jensyegeor()
				 + "/blogger";
		
	//	String dir =filex.read(baseDir + "/db/" + EditorForm.blogName.get());
	//	PropX px2=(PropX) EditorForm.propMap.get(EditorForm.blogName.get());
		String pubUrl =px.getProperty("pub_url");
		String blog_index=px.getProperty("blog_index");
		String pwd_enced=px.getProperty("pwd").trim();
		 
		String pwd =aes.decrypt(pwd_enced, "iluvmole");
		driver.findElement(By.id("password")).sendKeys(pwd);
	//	driver.findElement(By.id("remLoginName")).click();
		//
		try {
			driver.findElement(By.linkText("登录")).click();
		} catch (NoSuchElementException e) {
			driver.findElement(By.cssSelector("input.btn_submit_m")).click();
		}

		
	}

}
